package indi.a709924470.emergencybutton;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

/**
 * Created by Walter Ji on 2017/8/30.
 */

public class recycleViewhold extends RecyclerView.Adapter<recycleViewhold.ReViewHolder> {

    List<childlayout> li;
    Context ctx;
    public int clp = -1;

    public recycleViewhold(List<childlayout> mlist,Context con){
        this.li = mlist;
        this.ctx = con;
    }

    @Override
    public recycleViewhold.ReViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ReViewHolder(LayoutInflater.from(ctx).inflate(R.layout.child_contents, parent, false));
    }

    @Override
    public void onBindViewHolder(recycleViewhold.ReViewHolder holder, final int position) {
        holder.tv.setText(li.get(position).name + "\n" + li.get(position).pack);
        ImageLoader ild = new ImageLoader();
        ild.showimage(holder.img,li.get(position).img);
        holder.itemView.setBackgroundColor(position == clp ? Color.parseColor("#66CCFF") : Color.parseColor("#FAFAFA"));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clp = position;
                EmButton.pkgn = li.get(position).pack;
                EmButton.appIcon = li.get(position).img;
                Log.d("rvh", "Clicked: "+li.get(position).pack);
                EmButton.global_flag = true;
                notifyDataSetChanged();
            }
        });
    }

    @Override
    public int getItemCount() {return li.size();}

    class ReViewHolder extends RecyclerView.ViewHolder {
        TextView tv;
        ImageView img;
        public ReViewHolder(View itemView) {
            super(itemView);
            tv = (TextView) itemView.findViewById(R.id.appname);
            img = (ImageView) itemView.findViewById(R.id.appicon);
        }
    }
}
